package com.hhm.huo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hhm.huo.entity.Dictionary;
import com.hhm.huo.entity.Search;
import com.hhm.huo.service.IDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 安安
 * @since 2022-05-25
 */
@RestController
@RequestMapping("/dictionary")
public class DictionaryController {
    @Autowired
    IDictionaryService dictionaryService;

    @RequestMapping("list")
    public List<Dictionary> list (int type){
        QueryWrapper<Dictionary> wrapper = new QueryWrapper<>();
        wrapper.eq("type",type);
        List<Dictionary> list = dictionaryService.list(wrapper);
        return  list;
    }

    /**
     * 分页查询
     * @param search
     * @return
     */
    @RequestMapping("arealist")
    public  Page<Dictionary> listArea(@RequestBody Search search){
        QueryWrapper<Dictionary> wrapper = new QueryWrapper<>();// 组装查询条件
        wrapper.eq("type",5);//查询的是区域  等同于  where type=5
        // 模糊查询
        if(StringUtils.hasText(search.getKey())){  //StringUtils 字符串工具类
            wrapper.like("name",search.getKey()); // 等同于  name like '%key%'
        }
        // 分页
        Page<Dictionary> page = new Page<>(search.getPage(), search.getPageSize());
        Page<Dictionary> dictionaryPage = dictionaryService.page(page, wrapper);
        return  dictionaryPage;
    }

    /**
     * 下拉框4个
     * @return
     */
    @RequestMapping("list4")
    public List<Dictionary> list4 (){
        QueryWrapper<Dictionary> wrapper = new QueryWrapper<>();
        wrapper.in("type",1,2,3,4);
        List<Dictionary> list = dictionaryService.list(wrapper);
        return  list;
    }
}
